﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace IBS
{
    public partial class IbsDataContext
    {     
        public IEnumerable<RPO_DATA> GetRPO(long InvoiceID)
        {
            var sql = String.Format(@"SELECT
                                        LOCATION_KEY,
                                        S.COMPANY COMPANY,
                                        SO.ID_RECORD as InvoiceCode,
                                        '' AS OrderCode,
                                        (SELECT HQC_SHORTCODE FROM EDEX.TBHQCSUPPLIERCODE WHERE SITEID =LOCATION_KEY AND HQC_LONGCODE=COMPANY_KEY) AS SupplierCode,
                                        '1' AS StockLocationCode,
                                        DOC_DATE InvoiceDate,
                                        '1' AS InvoiceType,
                                        LINECOUNT as LinesNumber,
                                        TRIM(to_char(CAST(TOTAL_AMOUNT as NUMBER (8,2)),'99999999999.99')) TotalQTY,
                                        TRIM(to_char(SOL.QUANTITY,'99999999999.99')) AS InvoiceQuantity,
                                        TRIM(to_char(SOL.QUANTITY,'99999999999.99')) AS SupplyQuantity,
                                        '1' AS UOM,
                                        '1.00' AS UOMFactor,
                                        '' AS EmployeeCode,
                                        '.00' AS LineDiscount,
                                        '.00' AS InvoiceDiscount,
                                        '.00' AS InvoiceExtra,
                                        SOL.ACTUAL_PRICE as Cost,
                                        SO.NOTE as Notes,
                                        '' AS TemplateId,
                                        '' AS ItemName,
                                        '' AS OrderNumber,
                                        '' AS Remarks,
                                        SO.ID_RECORD as ExtendedInvoiceNumber,
                                        S.IP  
                                     FROM edex.WH_SALESORDER SO
                                     LEFT JOIN edex.WH_SALESORDERLINES SOL
                                     ON SO.ID_RECORD = SOL.ID_INVOICE
                                     LEFT JOIN HQCDBO.HQC_IMPORT_SITE HQC_SITE
                                     ON SO.LOCATION_KEY = HQC_SITE.HQC_SITE_ID
                                     LEFT JOIN IBSDBO.TBSITE S
                                     ON HQC_SITE.IBS_SITEIBSCODE = S.SITEIBSCODE
                             WHERE  SO.ID_RECORD = {0} ", InvoiceID);

            var data = new DB
                .Oracle(this.connectionString)
                .Excecute<RPO_DATA>(sql).AsEnumerable();     

            return data.AsEnumerable();
        }
        /// <summary>
        /// обновление статуса на "Отправлен"
        /// </summary>
        /// <param name="InvoiceID"></param>
        public void UpdateStatus(long InvoiceID)
        {
            //обновление статуса документа - "ОТПРАВЛЕН НА СТАНЦИЮ"
            var sql = String.Format(@"UPDATE edex.WH_SALESORDER SET STATUS = 1 WHERE ID_RECORD ={0}", InvoiceID);  
             new DB
                .Oracle(this.connectionString)
                .Excecute(sql);      
        }
    }
}
